import { container } from "tsyringe";
import { LISTENERS, ON_INITS, getCloudWs, getPushToken } from "../../tokens/core";
import { tryGet } from "../../utils/tryGet";
import { Sender } from "../sender";
import { CloudCodeListener } from "../listeners/cloudCodeListener";
import { setToken } from "../../core/logger";
import { getDeviceUuid } from "../../tokens";

export function mainInit() {
    container.register(LISTENERS, {
        useClass: CloudCodeListener
    });
    /**
     * sender 事件监听
     */
    container.register(ON_INITS, {
        useFactory: () => {
            return () => {
                setToken(getPushToken())
                const sender = tryGet(Sender)
                const ws = getCloudWs();
                sender.onInit();
                const uuid = getDeviceUuid()
                toast(`设备编号：${uuid}`)
                if (uuid) ws.connect();
            }
        }
    });
}


export function clientInit() {
    container.register(ON_INITS, {
        useFactory: () => {
            return () => {
                setToken(getPushToken())
            }
        }
    })
}
